package com.gwtcx.server.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.EntityTransaction;
import javax.persistence.TypedQuery;

import com.gwtcx.server.domain.User;

public class UserDao extends BaseDao {
	@Override
	public Long createObject(Object object) {
		return null;
	}

	@Override
	public List<Object> retrieveObjects(int maxResults, int firstResult) {
		return null;
	}
	
	public String createUser(User user) {
	    // For an application-managed entity manager its best practice to create a
	    // new entity manager inside a method and close it before the method is finished.
		  
		// When using transactions outside an enterprise application server you need to 
	    // close (commit or rollback) the transaction in the same way you do for EntityManagers.
		// In order for the resources (both EntityManager and Transaction) to be closed
		// you need to use an additional level of nesting.

		String login = "";
		EntityManager em = createEntityManager();

	    try {
	    	EntityTransaction tx = em.getTransaction();
	      
	    	try {
	    		tx.begin();
	    		em.persist(user);
	    		login = user.getLogin();
	    		tx.commit();
	    	} 
	    	finally {
	    		if (tx.isActive()) tx.rollback();
	    	}
	    } 
	    finally {
	    	em.close();
	    }    
	       
	    return login;
	}

	public User retrieveUser(String login) {
		EntityManager em = createEntityManager();
	    User user = null;

	    try {
	    	TypedQuery<User> query = em.createQuery("select u from User u where u.login = ?1", User.class);
	    	query.setParameter(1, login);
	    	user = query.getSingleResult();
	    }
	    finally {
	    	em.close();
	    }

	    return user;
	}
}
